package cn.com.klun.tjy.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
    public DateUtils() {
    }

    public static String getCurrDate() {
        String currentDate = null;
        currentDate = (new SimpleDateFormat("yyyyMMdd")).format(new Date());
        return currentDate;
    }

    public static String getCurrMonth() {
        String currentMonth = null;
        currentMonth = (new SimpleDateFormat("yyyyMM")).format(new Date());
        return currentMonth;
    }

    public static String getCurrYear() {
        String currentYear = null;
        currentYear = (new SimpleDateFormat("yyyy")).format(new Date());
        return currentYear;
    }

    public static String getCurrTime() {
        String currentTime = null;
        currentTime = (new SimpleDateFormat("HHmmss")).format(new Date());
        return currentTime;
    }

    public static String getCurrDateTime() {
        String currentDateTime = null;
        currentDateTime = (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date());
        return currentDateTime;
    }

    public static long getDateDiff(String date1, String date2) throws Exception {
//        boolean tempDifference = false;
        long difference = 0L;
        Date dateTmp1 = (new SimpleDateFormat("yyyyMMdd")).parse(date1);
        Date dateTmp2 = (new SimpleDateFormat("yyyyMMdd")).parse(date2);
        Calendar earlier = Calendar.getInstance();
        Calendar later = Calendar.getInstance();
        if (dateTmp1.compareTo(dateTmp2) < 0) {
            earlier.setTime(dateTmp1);
            later.setTime(dateTmp2);
        } else {
            earlier.setTime(dateTmp2);
            later.setTime(dateTmp1);
        }

        int tempDifference;
        while(earlier.get(1) != later.get(1)) {
            tempDifference = 365 * (later.get(1) - earlier.get(1));
            difference += (long)tempDifference;
            earlier.add(6, tempDifference);
        }

        if (earlier.get(6) != later.get(6)) {
            tempDifference = later.get(6) - earlier.get(6);
            difference += (long)tempDifference;
            earlier.add(6, tempDifference);
        }

        return dateTmp1.compareTo(dateTmp2) < 0 ? -difference : difference;
    }

    public static long getTimeDiff(String time1, String time2) throws Exception {
        Date d1 = (new SimpleDateFormat("yyyyMMddHHmmss")).parse(time1);
        Date d2 = (new SimpleDateFormat("yyyyMMddHHmmss")).parse(time2);
        long diff = d2.getTime() - d1.getTime();
        long time = diff / 1000L;
        return time;
    }

    public static String countTime(String time, int second) {
        String nextTime = null;

        try {
            SimpleDateFormat tstDate = new SimpleDateFormat("yyyyMMddHHmmss");
            Date tstdate1 = tstDate.parse(time);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(tstdate1);
            rightNow.add(13, second);
            nextTime = tstDate.format(rightNow.getTime());
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return nextTime;
    }

    public static String countDate(String currDate, int date) {
        String nextDate = null;

        try {
            SimpleDateFormat tstDate = new SimpleDateFormat("yyyyMMdd");
            Date tstdate1 = tstDate.parse(currDate);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(tstdate1);
            rightNow.add(5, date);
            nextDate = tstDate.format(rightNow.getTime());
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return nextDate;
    }

    public static String countMonth(String currMonth, int month) {
        String nextMonth = null;

        try {
            SimpleDateFormat tstDate = new SimpleDateFormat("yyyyMM");
            Date tstdate1 = tstDate.parse(currMonth);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(tstdate1);
            rightNow.add(2, month);
            nextMonth = tstDate.format(rightNow.getTime());
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return nextMonth;
    }

    public static boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    public static int getMonthDay(int year, int month) {
        int[] numDays = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int n = numDays[month - 1];
        if (month == 2) {
            n = isLeapYear(year) ? 29 : 28;
        }

        return n;
    }

    public static String getString_Date(String date) {
        String str = "";
        if (date != null) {
            str = date.replaceAll("-", "");
        }

        return str;
    }

    public static String get10_Date(String date) {
        String str = "";
        if (date != null) {
            try {
                str = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.subSequence(6, 8);
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

        return str;
    }

    public static String getFirstDate(String today) {
        String fristDate = null;

        try {
            SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
            Date day = formater.parse(today);
            Calendar c = Calendar.getInstance();
            c.setTime(day);
            c.set(5, 1);
            fristDate = formater.format(c.getTime());
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return fristDate;
    }

    public static String lastDateOfMonth(String today) {
        String lastDate = null;

        try {
            SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
            Date day = formater.parse(today);
            Calendar c = new GregorianCalendar();
            c.setTime(day);
            c.set(5, 1);
            c.add(2, 1);
            c.add(5, -1);
            lastDate = formater.format(c.getTime());
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return lastDate;
    }

    public static String getDate(String theDate, int days) {
        String endDay = null;

        try {
            SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
            Date day = formater.parse(theDate);
            Calendar c = new GregorianCalendar();
            c.setTime(day);
            c.add(5, days);
            endDay = formater.format(c.getTime());
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        return endDay;
    }

    public static int getDaysBetween(String startDate, String endDate) {
        Calendar calendarStartDate = Calendar.getInstance();
        Calendar calendarEndDate = Calendar.getInstance();
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        Date sDate = null;
        Date eDate = null;

        try {
            sDate = formater.parse(startDate);
            eDate = formater.parse(endDate);
        } catch (ParseException var9) {
            var9.printStackTrace();
        }

        calendarStartDate.setTime(sDate);
        calendarEndDate.setTime(eDate);
        if (sDate.after(eDate)) {
            Calendar swap = calendarStartDate;
            calendarStartDate = calendarEndDate;
            calendarEndDate = swap;
        }

        int days = calendarEndDate.get(6) - calendarStartDate.get(6);
        int y2 = calendarEndDate.get(1);

        while(calendarStartDate.get(1) < y2) {
            days += calendarStartDate.getActualMaximum(6);
            calendarStartDate.add(1, 1);
        }

        return days;
    }

    public static int getMonthsBetween(String startDate, String endDate) {
        Calendar calendarStartDate = Calendar.getInstance();
        Calendar calendarEndDate = Calendar.getInstance();
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        Date sDate = null;
        Date eDate = null;

        try {
            sDate = formater.parse(startDate);
            eDate = formater.parse(endDate);
        } catch (ParseException var8) {
            var8.printStackTrace();
        }

        calendarStartDate.setTime(sDate);
        calendarEndDate.setTime(eDate);
        if (sDate.after(eDate)) {
            Calendar swap = calendarStartDate;
            calendarStartDate = calendarEndDate;
            calendarEndDate = swap;
        }

        int months = calendarEndDate.get(2) - calendarStartDate.get(2) + (calendarEndDate.get(1) - calendarStartDate.get(1)) * 12;
        return months;
    }

    public static String getEndDateByDays(String startDate, int days) {
        String endDay = null;
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        Date day = null;

        try {
            day = formater.parse(startDate);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(day);
        c.add(6, days);
        endDay = formater.format(c.getTime());
        return endDay;
    }

    public static String getEndDateByMonths(String startDate, int months) {
        String endDay = null;
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        Date date = null;

        try {
            date = formater.parse(startDate);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(2, months);
        endDay = formater.format(c.getTime());
        return endDay;
    }

    public static String getEndDateByTerm(String startDate, int years, int months, int days) {
        return getEndDateByDays(getEndDateByMonths(startDate, years * 12 + months), days);
    }

    public static String getStartDateByDays(String endDate, int days) {
        String startDay = null;
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        Date day = null;

        try {
            day = formater.parse(endDate);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(day);
        c.add(6, 0 - days);
        startDay = formater.format(c.getTime());
        return startDay;
    }

    public static String getNextDay(String date) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        Date day = null;

        try {
            day = formater.parse(date);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(day);
        c.add(6, 1);
        String nextDay = formater.format(c.getTime());
        return nextDay;
    }

    public static String getLastDay(String date) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        Date day = null;

        try {
            day = formater.parse(date);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(day);
        c.add(6, -1);
        String lastDay = formater.format(c.getTime());
        return lastDay;
    }

    public static String getCurrDate(String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        return sd.format(new Date());
    }

    public static void main(String[] arg) throws ParseException {
        Date date1 = new Date();
        System.out.println("请求执行开始时间" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")).format(date1));
    }

    public static String getCurrDateHm() {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")).format(new Date());
    }

    public static String addDay(String date, int amount) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        Date day = null;

        try {
            day = formater.parse(date);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(day);
        c.add(6, amount);
        String nDay = formater.format(c.getTime());
        return nDay;
    }

    public static String addDays(String date, int amount) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss");
        Date day = null;

        try {
            day = formater.parse(date);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(day);
        c.add(6, amount);
        String nDay = formater.format(c.getTime());
        return nDay;
    }
}